package edu.pxd.cecs.ece510.pocketmanager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;

public class CreateActivity extends Activity {
	
	public static SQLiteDatabase db = null;
	private int eventPosition;
	private int count;
	private int track;
	private String EventTable = "EventTable";
	
	private  Button mCreateButton;
	 ListView list;
	    String[] web = {};
	    
	    String temp;
	    
	   public void screenUpdate(){
		   db = openOrCreateDatabase("EventDatabase", MODE_PRIVATE, null);
			//final String S = "INSERT INTO EventTable VALUES('" + eventname.getText().toString() + "', NULL);";
	    	db.execSQL("CREATE TABLE IF NOT EXISTS'" + EventTable + "'(eventname VARCHAR, id INTEGER PRIMARY KEY AUTOINCREMENT);");    	
			
			Cursor c = db.rawQuery("SELECT * from " + EventTable, null);
			count = c.getCount();
			c.moveToFirst();
			web = new String[count];
			for(track = 0;track < count; track++)
			{
				temp = c.getString(c.getColumnIndex("eventname"));
				web[track] = temp;
				c.moveToNext();
			}
	   }
	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create);
		screenUpdate();
		 final CustomList adapter = new CustomList(CreateActivity.this, web);		
	     list=(ListView)findViewById(R.id.list);
	     list.setAdapter(adapter);
	     list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
	    	 
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
            	eventPosition = position;
            	Intent i = new Intent(CreateActivity.this, ShowexpenseActivity.class);
    			i.putExtra(ShowexpenseActivity.EVENT_POSITION_KEY,(eventPosition+1));
    			startActivity(i);
    			finish();
                Toast.makeText(CreateActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
            }
	     });
	   
	     list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				
				/*for(track = arg2;track < (count - 1); track++)
					web[track] = web[track + 1];
				web[track] = null;
				adapter.notifyDataSetChanged();*/
				
				Cursor c = db.rawQuery("SELECT * from " + EventTable, null);
				c.moveToPosition(arg2);
				final String temp = "DELETE "+c.getString(c.getColumnIndex("eventname")).replaceAll(" ", "")+" FROM " +EventTable;
				//db.delete(EventTable, "eventname=" +c.getString(c.getColumnIndex("eventname")), new String[1]);
				Log.d("Name", c.getString(c.getColumnIndex("eventname")));
				//db.execSQL("delete "+c.getString(c.getColumnIndex("eventname"))+" FROM " +EventTable);
				db.delete(EventTable, "eventname=?", new String[] {c.getString(c.getColumnIndex("eventname"))});
				try{
					db.delete(c.getString(c.getColumnIndex("eventname")).replaceAll(" ", ""), null, null);
					//db.execSQL("DROP TABLE "+c.getColumnIndex("eventname"));
				}
				catch(SQLException exception){
					Log.d("Error", "Table not found");
				}
				//screenUpdate();
				onCreate(savedInstanceState);
				//db.execSQL("DELETE * FROM " +EventTable+ "' WHERE eventname = "+c.getString(c.getColumnIndex("eventname")));
				/*c.moveToFirst();
				web = null;
				count--;
				web = new String[count];
				for(track = 0;track < count; track++)
				{
					web[track] = c.getString(c.getColumnIndex("eventname"));
					c.moveToNext();
				}*/
				return false;
			}
		});
	    mCreateButton = (Button)findViewById(R.id.create_button);
	    mCreateButton.setOnClickListener(new View.OnClickListener() {
	    
	    @Override
		public void onClick(View v) {

	    	//db.close();
	    	Intent i = new Intent(CreateActivity.this, ExpensesActivity.class);
	    	startActivity(i);
			finish();
			}
	     });
	    	    
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.create, menu);
        return true;
    }
    
}
